<?php
/**
 * 检查 admin 模块的权限配置
 */

require __DIR__ . '/vendor/autoload.php';

// 初始化应用
$app = new think\App();
$app->initialize();

echo "=== 检查 admin 模块权限 ===\n\n";

// 模拟登录 admin
$_SESSION = [];
$_SESSION['user'] = [
    'id' => 10000,
    'username' => 'admin',
    'authorize' => '',
    'status' => 1
];

echo "【1】当前用户信息\n";
echo "  用户名: " . \think\admin\service\AdminService::getUserName() . "\n";
echo "  是否超级管理员: " . (\think\admin\service\AdminService::isSuper() ? '是' : '否') . "\n";
echo "  是否已登录: " . (\think\admin\service\AdminService::isLogin() ? '是' : '否') . "\n";

// 刷新权限
\think\admin\service\AdminService::apply(true);

echo "\n【2】测试各个节点的权限\n";

$nodes = [
    'admin/config/index',
    'admin/queue/index',
    'admin/oplog/index',
    'admin/base/index',
    'admin/file/index',
    'admin/menu/index',
    'admin/auth/index',
    'admin/user/index',
];

foreach ($nodes as $node) {
    $canAccess = \think\admin\service\AdminService::check($node);
    $result = $canAccess ? '✓ 允许' : '❌ 拒绝';
    echo "  {$node}: {$result}\n";
}

echo "\n【3】检查控制器注解\n";

// 检查几个关键控制器的注解
$controllers = [
    'app\\admin\\controller\\Config',
    'app\\admin\\controller\\Menu',
    'app\\admin\\controller\\Auth',
    'app\\admin\\controller\\User',
];

foreach ($controllers as $class) {
    if (class_exists($class)) {
        echo "\n  {$class}:\n";
        
        try {
            $reflection = new ReflectionClass($class);
            $docComment = $reflection->getDocComment();
            
            // 检查类注解
            if (preg_match('/@auth\s+(true|false)/i', $docComment, $matches)) {
                echo "    类 @auth: {$matches[1]}\n";
            }
            
            // 检查 index 方法注解
            if ($reflection->hasMethod('index')) {
                $method = $reflection->getMethod('index');
                $methodDoc = $method->getDocComment();
                
                if (preg_match('/@auth\s+(true|false)/i', $methodDoc, $matches)) {
                    echo "    index() @auth: {$matches[1]}\n";
                } else {
                    echo "    index() @auth: (未设置)\n";
                }
                
                if (preg_match('/@menu\s+(true|false)/i', $methodDoc, $matches)) {
                    echo "    index() @menu: {$matches[1]}\n";
                }
            }
        } catch (Exception $e) {
            echo "    错误: " . $e->getMessage() . "\n";
        }
    } else {
        echo "\n  {$class}: ❌ 类不存在\n";
    }
}

echo "\n【4】获取所有节点信息\n";

try {
    $methods = \think\admin\service\NodeService::getMethods(true);
    
    $adminNodes = array_filter($methods, function($node) {
        return strpos($node, 'admin/') === 0;
    }, ARRAY_FILTER_USE_KEY);
    
    echo "  admin 模块的节点数量: " . count($adminNodes) . "\n\n";
    
    echo "  部分节点详情：\n";
    $count = 0;
    foreach ($adminNodes as $node => $info) {
        if (in_array($node, $nodes)) {
            echo "  {$node}:\n";
            echo "    isauth: " . ($info['isauth'] ? 'true' : 'false') . "\n";
            echo "    islogin: " . ($info['islogin'] ? 'true' : 'false') . "\n";
            echo "    ismenu: " . ($info['ismenu'] ? 'true' : 'false') . "\n";
            $count++;
        }
        
        if ($count >= 5) break;
    }
    
} catch (Exception $e) {
    echo "  错误: " . $e->getMessage() . "\n";
}

echo "\n【5】直接测试 MenuService::getTree()\n";

try {
    $menus = \think\admin\service\MenuService::getTree();
    
    echo "  返回的一级菜单数量: " . count($menus) . "\n\n";
    
    foreach ($menus as $menu) {
        echo "  ├─ {$menu['title']} (ID:{$menu['id']})\n";
        
        if (!empty($menu['sub'])) {
            foreach ($menu['sub'] as $sub) {
                echo "  │  ├─ {$sub['title']} (ID:{$sub['id']})\n";
            }
        }
    }
    
    // 检查是否有"系统管理"
    $hasSysMenu = false;
    foreach ($menus as $menu) {
        if ($menu['title'] === '系统管理' || $menu['id'] == 1) {
            $hasSysMenu = true;
            echo "\n  ✓ 找到了系统管理菜单！\n";
            echo "  子菜单数量: " . (empty($menu['sub']) ? 0 : count($menu['sub'])) . "\n";
            break;
        }
    }
    
    if (!$hasSysMenu) {
        echo "\n  ❌ 没有找到系统管理菜单！\n";
        echo "  这说明菜单在 filter 过程中被过滤掉了\n";
    }
    
} catch (Exception $e) {
    echo "  错误: " . $e->getMessage() . "\n";
    echo "  堆栈: " . $e->getTraceAsString() . "\n";
}

echo "\n=== 检查完成 ===\n";

